package com.qdaily.ui;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.qdaily.supportlibrary.util.QLog;

/**
 * 检测activity状态的，主要用于后台切换到前台。
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 5/7/15
 * Time: 23:40
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class QDActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
    private int foregroundActivities;
    private boolean hasSeenFirstActivity;
    private boolean isChangingConfiguration;

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {
        foregroundActivities++;
        if (hasSeenFirstActivity && foregroundActivities == 1 && !isChangingConfiguration) {
            QLog.d("FELIX", "前台。");
        }
        hasSeenFirstActivity = true;
        isChangingConfiguration = false;
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        foregroundActivities--;
        if (foregroundActivities == 0) {
            QLog.d("FELIX", "后台");
        }
        isChangingConfiguration = activity.isChangingConfigurations();
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}
